package logo

import (
	"time"
)

var timeFormatter string = time.RFC3339

type Logger interface {
	// With data 可以是 error 或其它能够被 json 序列化的内容
	With(data interface{}) *ProxyLogger
	Debug(v ...interface{})
	Debugf(format string, v ...interface{})
	Info(v ...interface{})
	Infof(format string, v ...interface{})
	Warn(v ...interface{})
	Warnf(format string, v ...interface{})
	Error(v ...interface{})
	Errorf(format string, v ...interface{})

	SetLevel(level LogLevel) Logger
	GetLevel() LogLevel

	SetLogFileLine(log bool) Logger
	IsLogFileLine() bool

	SetFormatter(fmt Formatter) Logger
	SetPrinters(printers map[LogLevel]Printer) Logger
}

type LogLevel int

const (
	LevelDebug LogLevel = iota
	LevelInfo
	LevelWarn
	LevelError
)

var levels = []string{
	"debug", "info", "warn", "error",
}

func (l *LogLevel) MarshalIndent() ([]byte, error) {
	return []byte(levels[*l]), nil
}

func GetLevelString(level LogLevel) string {
	return levels[level]
}

type LogTime struct {
	time.Time
}

func SetTimeFormatter(formatter string) {
	timeFormatter = formatter
}

func (t *LogTime) MarshalIndent() ([]byte, error) {
	return []byte(t.Format(timeFormatter)), nil
}

type Entry struct {
	File string  `json:"file"`
	Line int     `json:"line"`
	Time LogTime `json:"time"` // 转换
	// TimeStr        string      `json:"time"`  //
	Level LogLevel `json:"level"` // 转换
	// LevelStr       string      `json:"level"` //
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
	Err     error       `json:"-"` // 转换
	// hideCallStacks bool        `json:"-"` // 不需要输出
	Error string `json:"error"`
}
